import { Component, OnInit ,ChangeDetectorRef } from '@angular/core';
import { StorageService } from '../../service/storage.service'
import { CommonService } from '../../service/common.service'
import { Router } from '@angular/router'
import { EventService } from '../../service/event.service'
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-withdraw',
  templateUrl: './withdraw.page.html',
  styleUrls: ['./withdraw.page.scss'],
})
export class WithdrawPage implements OnInit {

  constructor(private storage:StorageService,private common:CommonService,private alertController:AlertController,
    private changeDetectorRef:ChangeDetectorRef,private router:Router,private eventService:EventService) { }

  public money:number;
  public userInfo:any;
  public bank:string;
  public bankCard:string;
  
  public flag:boolean = false;
  public password:string;
  public show:boolean=false;
  public pwflag:boolean=false;

  ngOnInit() {
    this.common.getIdInfo()
    
    this.userInfo = this.common.getInfo()
    this.getPersonalInfo()
  }

  // 用户实名认证信息
  getPersonalInfo(){
    var api = '/tbr/getPersonalInfo'
    var params = {
      userId:this.userInfo.userId
    }
    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
        this.bank = res.data.bankName
        this.bankCard = res.data.bankCard
      }
    })
  }

  // 全部提现
  onTX(){
    this.money = this.userInfo.coin
  }

  onblur(){
    var patrn = /^\d+(\.\d+)?$/;
    if(!patrn.exec(this.money+'')){
      this.common.presentToast("请输入正确的金额")
      this.money = undefined
    }
  }

  //  提现申请
  confirm(){
    if(this.money < 100){
      this.common.presentAlert("金额不能低于100")
      return false
    }
    if(this.money == null || this.money == undefined){
      this.common.presentAlert("金额不能为空")
      return false
    }

    this.pwflag = true

  }

  closepw(){
    this.pwflag = false
  }

  // this called every time when user changed the code
  onCodeChanged(code: string) {
  }

  // this called only if user entered full code
  onCodeCompleted(code: string) {
    this.show=true
    // console.log(code)
    // debugger
    if(code == null || code == ''){
      this.common.presentAlert("密码不能为空")
      return false
    } 
    if(code.length < 6){
      this.common.presentAlert("密码不能少于6位数")
      return false
    } 
    var money;
      var number = (this.money.toString()).indexOf(".");
      if(number!= -1) {
        money = +this.money
        // console.log("有"+money)
      } else {
            money = +this.money
            // console.log(money)
      }

      var api = '/tbr/withdrawCash'
      // console.log(this.money)
      var params = {
        userId:this.userInfo.userId,
        cashNumber:money.toFixed(2),
        paymentCode:code
      }
      this.common.post(api,params,false).then((res:any)=>{
         
          if(res.code == 200){
            this.show=false
            this.pwflag = false
            this.userInfo.coin = this.userInfo.coin - this.money
            this.events()
            this.router.navigate(['/success'],{
              queryParams: {
                title:"提现申请提交成功",
                coin:this.money,
                url:'/tabs/tab3'
              }
            })
            this.money = null
          }
      })
  }

  //通知用户中心更新用户信息
  events(){
    this.eventService.event.emit('userstate');
  }

}
